/*
 * Copyright (c) 2011 Ayan Dave http://www.linkedin.com/in/daveayan
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 * associated documentation files (the "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial 
 * portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package testrj.domain;

import java.util.ArrayList;
import java.util.List;

public class DomainObject {
	protected Object parameter;
	protected String type;
	protected String referenceName;
	protected transient SerializedData serializedData = new VoidSerializedData();

	public boolean isParameterUpdated() {
		return !serializedData.equals(TestrjConfig.data_serializer.serialize(parameter));
	}

	public DomainObject reserialize() {
		DomainObject newDomainObject = new DomainObject();
		newDomainObject.setParameter(parameter);
		return newDomainObject;
	}

	public boolean isSymbolicPrimitive() {
		if (getParameter().getClass().isPrimitive() || !getSerializedData().toString().contains("\"class\":"))
			return true;
		return false;
	}

	public static DomainObject asDomainObject(Object object) {
		if (object == null) {
			return nullValue();
		} else {
			DomainObject domainObject = new DomainObject();
			domainObject.setParameter(object);
			return domainObject;
		}
	}

	public static List<DomainObject> listFromArrayOfParameters(Object[] inputParams) {
		List<DomainObject> listOfDomainObjects = new ArrayList<DomainObject>();
		for (Object param : inputParams) {
			listOfDomainObjects.add(asDomainObject(param));
		}
		return listOfDomainObjects;
	}

	public boolean isVoid() {
		return false;
	}

	public boolean isNull() {
		return false;
	}

	public Object getParameter() {
		return parameter;
	}

	public void setParameter(Object parameter) {
		this.parameter = parameter;
		if (parameter != null) {
			this.type = parameter.getClass().getName();
			this.serializedData = TestrjConfig.data_serializer.serialize(parameter);
		}
	}

	public String className() {
		return this.getParameter().getClass().getName();
	}

	public String getType() {
		return type;
	}

	public String getReferenceName() {
		return referenceName;
	}

	public void setReferenceName(String referenceName) {
		this.referenceName = referenceName;
	}

	public static DomainObject voidValue() {
		return new VoidValue();
	}

	public static DomainObject nullValue() {
		return new NullValue();
	}

	public SerializedData getSerializedData() {
		return serializedData;
	}
}

class VoidValue extends DomainObject {
	public VoidValue() {
		type = "void";
		serializedData = new VoidSerializedData();
	}

	public boolean isParameterUpdated() {
		return false;
	}

	public boolean isSymbolicPrimitive() {
		return false;
	}

	public String className() {
		return "void";
	}

	public boolean isVoid() {
		return true;
	}

	public boolean isNull() {
		return false;
	}
}

class NullValue extends DomainObject {
	public NullValue() {
		type = "null";
		serializedData = new NullSerializedData();
	}

	public boolean isParameterUpdated() {
		return false;
	}

	public boolean isSymbolicPrimitive() {
		return false;
	}

	public String className() {
		return "null";
	}

	public boolean isVoid() {
		return false;
	}

	public boolean isNull() {
		return true;
	}
}